package com.lingnan.controller;

import com.lingnan.service.UserStatisticsService;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping("/statistics/users")
public class UserStatisticsController {

    private final UserStatisticsService statisticsService;

    /**
     * 获取用户总量统计
     * @since Java 8 compatible
     */
    @GetMapping("/counts")
    public Map<String, Long> getUserCounts() {
        Map<String, Long> counts = new HashMap<>();
        counts.put("jobSeekers", statisticsService.getJobSeekerCount());
        counts.put("companies", statisticsService.getCompanyUserCount());
        counts.put("hrs", statisticsService.getHrUserCount());
        return counts;
    }

    /**
     * 获取用户增长统计
     * @param days 查询天数（默认7天）
     * @since Java 8 compatible
     */
    @GetMapping("/growth")
    public Map<String, Long> getUserGrowth(
            @RequestParam(defaultValue = "7") int days) {
        return statisticsService.getUserGrowth(days);
    }

    /**
     * 按月统计用户数量
     * @param month 月份 (1-12)
     * @since Java 8 compatible
     */
    @GetMapping("/monthly")
    public Map<String, Long> getMonthlyCounts(
            @RequestParam int month) {
        Map<String, Long> monthlyCounts = new HashMap<>();
        monthlyCounts.put("jobSeekers", statisticsService.getJobSeekerCountByMonth(month));
        monthlyCounts.put("companies", statisticsService.getCompanyUserCountByMonth(month));
        monthlyCounts.put("hrs", statisticsService.getHrUserCountByMonth(month));
        return monthlyCounts;
    }

    /**
     * 自定义日期范围统计
     * @since Java 8 compatible
     */
    @GetMapping("/custom-range")
    public Map<String, Long> getByDateRange(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate end) {
        // 需要新增Service方法
        Map<String, Long> result = new HashMap<>();
        result.put("jobSeekers", statisticsService.countJobSeekersByDateRange(start, end));
        result.put("companies", statisticsService.countCompaniesByDateRange(start, end));
        result.put("hrs", statisticsService.countHrsByDateRange(start, end));
        return result;
    }

    /**
     * 获取用户增长百分比
     * @param days 查询天数（默认7天）
     */
    @GetMapping("/growth-percentage")
    public Map<String, String> getUserGrowthPercentage(
            @RequestParam(defaultValue = "7") int days) {
        return statisticsService.getUserGrowthPercentage(days);
    }
}

